import r from './r.js';

//后台服务器根目录
export const $getAPIBaseUrl = (key) => {
    // console.info(key)
    let projectenv = $getProjectenv()
    let n = $isWaiwangFlag()
        // console.info(r.backstage[key][projectenv])
        // console.info(`当前后台：${key}：${r.backstage[key][projectenv][n]}`)
    return $isWaiwang() ? r.backstage[key][projectenv][n] : r.backstage[key][r.projectenv][n];
}

export const $getProjectenv = () => {

    let host = location.href
    if (host.indexOf("xxxxxxxx") != -1 || host.indexOf("192.168.2.204") != -1) {
        return "1"
    } else if (host.indexOf("117.149.225.15") != -1) {
        return "2"
    } else {
        return r.projectenv
    }
}

export const $isWaiwangFlag = () => {
    let n = $isWaiwang() ? 'w' : 'n'
    return n
}

    //dev下面永远是读取w字段
    //判断是线上的文件还是线下的文件
export const $isWaiwang = () => {
    let host = location.host;
    return host.indexOf('10.20') === -1
        // let isProdEnv = !(host.indexOf('192.168.') > -1 || host.indexOf('localhost') > -1 || host.indexOf('172.16') > -1)
        // isProdEnv = true
        // return isProdEnv
}

export const $apiAjax = (url, param, success, error, method) => {
    if (!param.userId) { param.userId = $globalData().userInfo.userId || 1; }
    if ($globalData().userInfo.unionid){
      param.unionid = $globalData().userInfo.unionid;
    }
    console.log(param);
    var header={};
    if (method == 'post') 
    { header = { 'content-type': 'application/x-www-form-urlencoded'};}
    wx.request({
      url: url,
      data: param,
      header: header,
      method: method || 'GET',
      success: (res) => {
        wx.hideLoading();
        wx.hideToast();
        console.log(res);
        var result = res.data
        if (result.code == 200) {
          success && success(res.data);
        } else {
          var tips = '';
          if (result.code != 200) { tips = 'error: ' + result.code; }
          else if (res.data) { tips = 'error: ' + JSON.stringify(res.data); }
          else { tips = 'error: interface program'; }
          console.log(tips);
          $toast(tips)
          console.log(res);
          error && error({ errMsg: tips });
        }
      },
      fail:(err)=>{
        console.log(err);
        wx.hideLoading();
        wx.hideToast();
        if (error) error(err);
        else $toast('连接失败' + err.errMsg)
      }
    })
  }

  export const $globalData = () => {
     let globalObject = {
        // apiUrl: 'http://192.12.12.6:8081/yd/',
        // apiUrl: 'http://47.110.40.176:8081/yd/',
        apiUrl: 'https://test.yuncai58.top/yd/',
        // apiUrl: 'https://ttx.yuncai58.top/yd/',
        page_size: 20,//分页接口每页返回数据条数--与后端统一
        openid:'',
        sessionKey:'',
        userInfo: {},
        shopInfo:{},
        shop_qrcode:{},
        imageSrc:'http://jczg.oss-cn-hangzhou.aliyuncs.com/yd',
        publicImg: 'https://taotongxue.oss-cn-hangzhou.aliyuncs.com/ttxsystemimg/ttxlogo.jpg'
      }
      return globalObject;
  }

    // toast提示
    export const $toast = (msg,time) => {
        if(msg){
          setTimeout(function(){
            wx.showToast({
              icon: 'none',
              title: msg,
            })
          },time||0)
        }
        console.log('toast文字',msg)
      }